package com.fly.bean.result;

import com.fly.bean.enums.CodeEnum;
import lombok.Data;
import java.io.Serializable;

/**
 * 通用返回结果封装（支持链式调用）
 * @param <T> 泛型数据类型
 */
@Data
public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;   // 状态码
    private String msg;     // 提示消息
    private T data;         // 返回数据

    // 快速成功（无数据）
    public static <T> CommonResult<T> ok() {
        return ok(null);
    }

    // 成功（带数据）
    public static <T> CommonResult<T> ok(T data) {
        return ok(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), data);
    }

    // 完全自定义成功
    public static <T> CommonResult<T> ok(Integer code, String msg, T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        commonResult.setData(data);
        return commonResult;
    }

    // 快速失败
    public static <T> CommonResult<T> fail() {
        return fail(CodeEnum.FAIL.getCode(), CodeEnum.FAIL.getMsg());
    }

    // 失败（自定义消息）
    public static <T> CommonResult<T> fail(String msg) {
        return fail(CodeEnum.FAIL.getCode(), msg);
    }

    // 完全自定义失败
    public static <T> CommonResult<T> fail(Integer code, String msg) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    // 链式调用支持（可选）
    public CommonResult<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public CommonResult<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public CommonResult<T> data(T data) {
        this.data = data;
        return this;
    }
}